﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.DTO;
using Microhard.Core;
using Microhard.Core.DBUtility;
using Microhard.WarehouseInsp.Buz.Msg;
using System.IO;

namespace Microhard.WarehouseInsp.Buz.Impls
{
    public class FileImpl : IFile
    {

        public Core.DBUtility.ExecutedResult Add(AttachmentModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();

            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            db.Attachments.AddObject(op);

            try
            {
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, AttachmentMsg.AddFailed);
                Log.Error("IFile.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(AttachmentModel model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult rtn = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();
            var entities = db.Attachments.Where(f => ids.Contains(f.AttachmentID));
            foreach (var e in entities)
            {
                db.Attachments.DeleteObject(e);
            };
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, AttachmentMsg.DeleteFailed);
                Log.Error("IFile.RemoveFormDB", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Delete(AttachmentModel model)
        {
            throw new NotImplementedException();
        }

        public AttachmentModel GetModelByID(Guid id)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            var entity = db.Attachments.SingleOrDefault(f => f.AttachmentID == id);
            if (entity != null)
                return entity.ToModel();
            return null;
        }

        public AttachmentModel GetModelByModel(AttachmentModel model)
        {
            throw new NotImplementedException();
        }

        public List<AttachmentModel> Query(AttachmentModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<AttachmentModel> Query(AttachmentModel Model)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            var list = db.Attachments.Where(a => a.MainDataID == Model.MainDataID);
            if (!string.IsNullOrEmpty(Model.FileType))
            {
                list = db.Attachments.Where(a => a.MainDataID == Model.MainDataID && a.FileType == Model.FileType);
            }
            if (list.Count() > 0)
            {
                return (from l in list
                        select new AttachmentModel
                        {
                            AttachmentID = l.AttachmentID,
                            CreateTime = l.CreateTime.Value,
                            FileType = l.FileType,
                            MainDataID = l.MainDataID.Value,
                            Name = l.Name,
                        }).ToList();
            }
            return new List<AttachmentModel>();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public ExecutedResult AddBigFile(AttachmentModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            WarehouseInspEntities db = new WarehouseInspEntities();

            byte[] filedata = model.Content;
            model.Content = null;
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            db.Attachments.AddObject(op);
            string filePath = System.Configuration.ConfigurationManager.AppSettings["BigFileFolder"] + model.Name;
            op.FullFilePath = filePath;
            op.HasFullFilePath = true;

            try
            {
                db.SaveChanges();
                //保存成文件
                File.WriteAllBytes(filePath, filedata);
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, AttachmentMsg.AddFailed);
                Log.Error("IFile.Add", ex);
            }
            return rtn;
        }


        public AttachmentModel GetBigFilePath(Guid attachmentID)
        {
            WarehouseInspEntities db = new WarehouseInspEntities();
            var entity = db.Attachments.SingleOrDefault(f => f.AttachmentID == attachmentID);
            if (entity != null)
                return new AttachmentModel
                {
                    FullFilePath = entity.FullFilePath,
                    HasFullFilePath = entity.HasFullFilePath.Value,
                    Name = entity.Name,
                    CreateTime = entity.CreateTime.Value,
                    AttachmentType = entity.AttachmentType.Value,
                    FileType = entity.FileType,
                    MainDataID = entity.MainDataID.Value
                };
            return new AttachmentModel();
        }
    }
}
